/*
 * To change this template, choose Tools | Templates
 * and open the template in the editor.
 */
package Action;

import Entity.Category;
import java.util.List;
import javax.ejb.Stateless;
import javax.persistence.EntityManager;
import javax.persistence.PersistenceContext;
import javax.persistence.PrePersist;
import javax.persistence.Query;

/**
 *
 * @author Cyrax
 */
@Stateless
public class CategorySessionBean implements CategorySessionBeanLocal {

    @PersistenceContext
    EntityManager em;

     boolean result = false;
    public void persist(Object object) {
        em.persist(object);
    }
    Category cat = null;

    @Override
    public List<Category> listAllCategory() {
        List<Category> ct = em.createNamedQuery("Category.findAll").getResultList();
        return ct;
    }

    @Override
    public void insertCategory(String nameCategory) {
        Category cat;
        cat = new Category();
        cat.setCategoryName(nameCategory);
        em.persist(cat);
    }

    @Override
    public void updateCategory(int idCategory, String categoryName) {

        cat = em.find(Category.class, idCategory);
        if (cat != null) {
            cat.setCategoryName(categoryName);
            cat.setCategoryId(idCategory);
            em.persist(cat);
        } else {
            System.out.println("Not found category ID to Update");
        }

    }

    @Override
    public void deleteCategory(Category cat) {

        if (cat != null) {
            em.remove(em.merge(cat));
        } else {
            System.out.println("Not found category ID to delete");
        }
    }

    @Override
    public void addCategory(Category cat) {
        try {

            em.persist(cat);
        } catch (Exception e) {
        }
    }

    @Override
    public boolean deleteCat(int idCategory) {
        Query q = em.createNamedQuery("Category.delete");
        q.setParameter("categoryId", idCategory);
        return (q.executeUpdate() == 1) ? true : false;
    }

    @Override
    public List<Category> searchCategory(String categoryName) {
        List<Category> cat = null;

        cat = em.createQuery("SELECT c FROM Category c WHERE c.categoryName like :categoryName").setParameter("categoryName", "%" + categoryName + "%").getResultList();
        System.out.println("categoryName : " + categoryName);


        return cat;
    }

    @Override
    public void categoryUpdate(Category cat) {

        try {
            em.getTransaction().begin();
            Category cate = em.find(Category.class, cat.getCategoryId());
            cate.setCategoryName(cat.getCategoryName());

            em.getTransaction().commit();
        } finally {
            em.close();
           
        }
    }
     @Override
    public boolean checkCategoryName(String name)
    {
      List<Category> catList=null;
         catList=em.createQuery("SELECT a FROM Category a").getResultList();
         for(Category cat: catList)
         {
           if(cat.getCategoryName().equals(name))
           {
              result=true;
              break;
           }else
           {
               result=false;
           }
         }
        return  result;
    }
}
